package myschool.test;

import myschool.dao.SubjectDao;
import myschool.entity.Subject;
import myschool.kit.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class SubjectDaoTest1 {
    public static void main(String[] args) {
        SqlSession sqlSession = MybatisUtil.getSqlSession(true);
        SubjectDao mapper = sqlSession.getMapper(SubjectDao.class);
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("请选择您需要的操作：\n1、增\n2、改\n3、删\n4、查课程总数\n5、根据id查课程\n6、分页查询\n0、退出");
            int chooce = input.nextInt();
            if (chooce == 1) {
                System.out.println("请输入新课程名称：");
                String sbname = input.next();
                System.out.println("请输入新课程的学时：");
                int classhour = input.nextInt();
                System.out.println("请输入新课程的年级编号：");
                int gradeid = input.nextInt();
                int add = mapper.add(new Subject(sbname, classhour, gradeid));
                System.out.println(add > 0 ? "增加成功！" : "增加失败！");
            } else if (chooce == 2) {
                System.out.println("请输入需要修改的课程编号：");
                int id = input.nextInt();
                System.out.println("请输入新课程名称：");
                String sbname = input.next();
                System.out.println("请输入新课程的学时：");
                int classhour = input.nextInt();
                System.out.println("请输入新课程的年级编号：");
                int gradeid = input.nextInt();
                int update = mapper.update(new Subject(id, sbname, classhour, gradeid));
                System.out.println(update > 0 ? "修改成功！" : "修改失败！");
            } else if (chooce == 3) {
                System.out.println("请输入需要删除的课程编号：");
                int id = input.nextInt();
                int delete = mapper.delete(id);
                System.out.println(delete > 0 ? "删除成功！" : "删除失败！");
            } else if (chooce == 4) {
                int total = mapper.findTotal();
                System.out.println("总课程数：" + total);
            } else if (chooce == 5) {
                System.out.println("请输入需要查询的课程编号：");
                int id = input.nextInt();
                Subject subject = mapper.find(id);
                System.out.println(subject);
            } else if (chooce == 6) {
                System.out.println("请输入需要查询的页数：");
                int page = input.nextInt();
                System.out.println("请输入每页显示行数：");
                int line = input.nextInt();
                page=(page-1)*line;
                List<Subject> list = mapper.findALL(page,line);
//                HashMap<String, Integer> si = new HashMap<>();
//                si.put("page",(page-1)*line);
//                si.put("line",line);
//                List<Subject> list = mapper.findAll(page,line);
                for (Subject s : list) {
                    System.out.println(s);
                }
            } else {
                MybatisUtil.close(sqlSession);
                return;
            }
        }
    }
}
